package org.springframework.prospring.ticket.web;

import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.util.WebUtils;
import org.springframework.prospring.ticket.service.*;
import org.springframework.prospring.ticket.domain.location.Region;
import org.springframework.prospring.ticket.common.Constant;
import org.springframework.validation.Errors;
import org.springframework.validation.BindException;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.Map;
import java.util.HashMap;
import java.text.SimpleDateFormat;
import java.text.ParseException;

/**
 * Form controller issuing a ReservationRequest on behalf of the user.
 * The user has selected a show, a price band and a date in the previous
 * screen(s) and this form controller will create the reservation
 * request based on this. Together with the amount of seats the user will
 * be selected, this form controller completes the request.<p>
 *
 * Input:
 * <ul>
 *   <li><code>performanceId</code>: the performance identifier</li>
 *   <li><code>priceBandId</code>: the price band identifier</li>
 * </ul>
 * Error conditions:
 * <ul>
 *  <li>When not enough seats are available anymore</li>
 * </ul>
 */
public abstract class AbstractReservationFieldRequestController extends SimpleFormController {

    // the next view name, or controler forward.
    private String viewForward;

    // the Club manager service this controller uses.
    protected ClubManager clubManager;

    // the Region manager service this controller uses.
    protected RegionManager regionManager;

    // the Sport manager service this controller uses.
    protected SportManager sportManager;

    // the User manager service this controller uses.
    protected UserManager userManager;

    protected ReservationFieldRequest getReservationRequestFromSession(HttpServletRequest request) {

        ReservationFieldRequest reservationFieldRequest = (ReservationFieldRequest) WebUtils.getSessionAttribute(request, "reservationFieldRequest");

        if (reservationFieldRequest == null){
            reservationFieldRequest = new ReservationFieldRequest();
        }
        String regionId = request.getParameter("regionId");
        String clubId = request.getParameter("clubId");
        String fieldId = request.getParameter("fieldId");
        String reservationDate = request.getParameter("reservationDate");
        String startHour = request.getParameter("startHour");
        String endHour = request.getParameter("endHour");

        Collection sports = sportManager.findAllSports();
        request.getSession().getServletContext().setAttribute("sports", sports);

        Collection<Region> regions = regionManager.findAllRegions();
        reservationFieldRequest.setRegions(regions);


        if (StringUtils.isNotEmpty(regionId))
            reservationFieldRequest.setRegionId(Long.valueOf(regionId));
        if (StringUtils.isNotEmpty(clubId))
            reservationFieldRequest.setClubId(Long.valueOf(clubId));
        if (StringUtils.isNotEmpty(fieldId))
            reservationFieldRequest.setFieldId(Long.valueOf(fieldId));
        if (StringUtils.isNotEmpty(reservationDate)){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constant.DATE_FORMAT_KEY);
            try {
                reservationFieldRequest.setReservationDate(simpleDateFormat.parse(reservationDate));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (StringUtils.isNotEmpty(startHour))
            reservationFieldRequest.setStartHour(startHour);
        if (StringUtils.isNotEmpty(endHour))
            reservationFieldRequest.setEndHour(endHour);

        return reservationFieldRequest;
    }

    public String getViewForward() {
        return viewForward;
    }

    public void setViewForward(String viewForward) {
        this.viewForward = viewForward;
    }

    public ClubManager getClubManager() {
        return clubManager;
    }

    public void setClubManager(ClubManager clubManager) {
        this.clubManager = clubManager;
    }

    public RegionManager getRegionManager() {
        return regionManager;
    }

    public void setRegionManager(RegionManager regionManager) {
        this.regionManager = regionManager;
    }

    public SportManager getSportManager() {
        return sportManager;
    }

    public void setSportManager(SportManager sportManager) {
        this.sportManager = sportManager;
    }

    public UserManager getUserManager() {
        return userManager;
    }

    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }

}
